import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
Vue.use(VueRouter);

const routes = [{
        path: "/home",
        name: "home",
        component: HomeView,
        children: [{
                path: "/home/1-1",
                name: "HomePage",
                meta: ["主页"],
                component: () =>
                    import("../views/HomePage.vue")
            }, {
                path: "/home/2-1",
                name: "User",
                meta: ["系统管理", "用户管理"],
                component: () =>
                    import("../views/manage/User.vue")
            }, {
                path: "/home/2-2",
                name: "Role",
                meta: ["系统管理", "角色管理"],
                component: () =>
                    import("../views/manage/Role.vue")
            }, {
                path: "/home/2-3",
                name: "Menu",
                meta: ["系统管理", "菜单管理"],
                component: () =>
                    import("../views/manage/Menu.vue")
            }, {
                path: "/home/2-4",
                name: "Branch",
                meta: ["系统管理", "部门管理"],
                component: () =>
                    import("../views/manage/Branch.vue")
            }, {
                path: "/home/2-5",
                name: "Dictionaries",
                meta: ["系统管理", "字典管理"],
                component: () =>
                    import("../views/manage/Dictionaries.vue")
            }, {
                path: "/home/2-6",
                name: "Log",
                meta: ["系统管理", "行为日志"],
                component: () =>
                    import("../views/manage/Log.vue")
            },
            {
                path: "/home/3-1",
                name: "Code",
                meta: ["开发中心", "代码生成"],
                component: () =>
                    import("../views/exploit/Code.vue")
            }, {
                path: "/home/3-2",
                name: "Form",
                meta: ["开发中心", "表单构建"],
                component: () =>
                    import("../views/exploit/Form.vue")
            }, {
                path: "/home/3-3",
                name: "Data",
                meta: ["开发中心", "数据接口"],
                component: () =>
                    import("../views/exploit//Data.vue")
            }
        ]
    },
    {
        path: "/about",
        name: "about",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import( /* webpackChunkName: "about" */ "../views/AboutView.vue"),
    },
    {
        path: "/",
        name: "login",
        component: () =>
            import("../views/MyLogin")
    }


];

const router = new VueRouter({
    routes,
});

router.beforeEach(function (to, from, next) {
    // console.log(to, from)
    if (to.name === "login") {
        next()
    } else {
        let token = localStorage.getItem('token')
        if (!token) return next({
            path: "/login"
        })
        if (token) return next()
        if (token) { //有权限
            next()
        }
    }
})

export default router;